<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-calendar"></i>系统设置</el-breadcrumb-item>
                <el-breadcrumb-item>密码修改</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="form-box">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px">
                    <el-form-item label="当前密码" prop="oldPwd">
                        <el-input type="password" v-model="ruleForm.oldPwd" placeholder="请输入旧密码" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="newPwd">
                        <el-input type="password" v-model="ruleForm.newPwd" placeholder="请输入6-18位数字和字母组合的密码"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="checkPwd">
                        <el-input type="password" v-model="ruleForm.checkPwd" placeholder="请再次输入新密码"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit()">确定</el-button>
                        <el-button @click="resetForm()">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: 'checkPwd',
        data: function(){
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                  callback(new Error('请输入正确的新密码!'));
                } else {
                  if (this.ruleForm.checkPwd !== '') {
                    this.$refs.ruleForm.validateField('checkPwd');
                  }
                  callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                  callback(new Error('请输入正确的新密码!'));
                } else if (value !== this.ruleForm.newPwd) {
                  callback(new Error('请输入正确的新密码!'));
                } else {
                  callback();
                }
            };
            return {
                ruleForm: {
                    oldPwd: '', // 密码 长度位6-18位(前后闭区间)数字和字母组合的密码
                    newPwd: '', // 新密码
                    checkPwd: '' // 确认密码
                },
                rules: {
                    oldPwd: [
                        { required: true, message: '请输入正确的原密码', trigger: 'blur' },
                        { min: 6, max: 18, message: '请输入正确格式的密码',pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/, trigger: 'blur' }
                    ],
                    newPwd: [
                        { required: true,validator: validatePass, trigger: 'blur' },
                        { min: 6, max: 18, message: '请输入正确格式的密码',pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/, trigger: 'blur' }
                    ],
                    checkPwd: [
                        { required: true,validator: validatePass2, trigger: 'blur' },
                        { min: 6, max: 18, message: '请输入正确格式的密码',pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/, trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            onSubmit(formName) {
                this.$refs.ruleForm.validate(async (valid) => {
                    console.log(valid)
                    if (valid) {
                        let res = await this.request('modiftyPwd', this.form)
                        // this.setUsrInfo({uname: this.loginForm.username})
                        // this.$router.push('/');
                        // this.$message.success('修改成功！');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            },
            resetForm(formName) {
                this.$refs.ruleForm.resetFields();
            }
        }
    }
</script>
